<?php


class Pagination {

	var $adjacents = 3;
	var $PagiCurrent;
	var $PagiCurrentIndex;
	var $PagiLimit;
	var $PagiTotal;
	var $PagiOffset;
	var $PagiLinkUrl;
	var $PagiLinkHtml = '<a href="%URL%&p=%PAGE%" class="pageno" style="padding: 0 3px;" >%PAGE%</a>';
	var $PagiCurrentHtml = '<span class="pageno" style="padding: 0 3px;">%PAGE%</span>';

	function __construct()
	{


	}

	function getLimit()
	{
		return $this->PagiLimit;
	}
	function setLimit($num)
	{
		$this->PagiLimit = $num;
	}

    function setLinkUrl($url)
    {
        $this->PagiLinkUrl = $url;
    }

    function setTotal($num)
    {
		$this->PagiTotal = $num;
	}


	function setCurrentPage($p=1)
	{
		$this->PagiCurrent = $p;
		$this->setCurrentIndex();
	}

	function setCurrentIndex()
	{
		if( $this->PagiCurrent > 0 )
			$this->PagiCurrentIndex = ( $this->PagiCurrent - 1 ) * $this->PagiLimit;
		else
			$this->PagiCurrentIndex = 0;
	}
	function getCurrentIndex()
	{
		return $this->PagiCurrentIndex;
	}
	function generate()
	{
		$pagination = "<div class=\"pagination\">";
		$prev = $this->PagiCurrent - 1;
		$next = $this->PagiCurrent + 1;
		$lastpage = ceil($this->PagiTotal / $this->PagiLimit);
		$lpm1 = $lastpage - 1;
		$page = $this->PagiCurrent;

		//prev button
		if( $this->PagiCurrent > 1 )
		{
			$pagination .= '<a href="'.$this->PagiLinkUrl.'&p='.$prev.'" text="&lt; 上一頁" style="padding: 0 3px;">上一頁</a>';
		}else
		{
			$pagination .= '<span class="disabled" style="padding: 0 3px;">上一頁</span>';
		}

		//$pagination .=	'<ul style="float:left;">';
		//pages
		if ($lastpage < 7 + ($this->adjacents * 2))	//not enough pages to bother breaking it up
		{

			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination .= $this->generateCurrentStringForPage($counter);
				else
					$pagination .= $this->generateLinkStringForPage($counter);
			}
		}
		elseif($lastpage > 5 + ($this->adjacents * 2))	//enough pages to hide some
		{

			//close to beginning; only hide later pages
			if($page < 1 + ($this->adjacents * 2))
			{

				for ($counter = 1; $counter < 4 + ($this->adjacents * 2); $counter++)
				{
					if ($counter == $page)
						$pagination .= $this->generateCurrentStringForPage($counter);
					else
						$pagination .= $this->generateLinkStringForPage($counter);
				}
				$pagination .= "...";
				$pagination .= $this->generateLinkStringForPage($lpm1);
				$pagination .= $this->generateLinkStringForPage($lastpage);
			}
			//in middle; hide some front and some back
			elseif($lastpage - ($this->adjacents * 2) > $page && $page > ($this->adjacents * 2))
			{

				$pagination .= $this->generateLinkStringForPage(1);
				$pagination .= $this->generateLinkStringForPage(2);
				$pagination .= "...";
				for ($counter = $page - $this->adjacents; $counter <= $page + $this->adjacents; $counter++)
				{
					if ($counter == $page)
						$pagination .= $this->generateCurrentStringForPage($counter);
					else
						$pagination .= $this->generateLinkStringForPage($counter);
				}
				$pagination .= "...";
				$pagination .= $this->generateLinkStringForPage($lpm1);
				$pagination .= $this->generateLinkStringForPage($lastpage);
			}
			//close to end; only hide early pages
			else
			{

				$pagination .= $this->generateLinkStringForPage(1);
				$pagination .= $this->generateLinkStringForPage(2);
				$pagination .= "...";
				for ($counter = $lastpage - (2 + ($this->adjacents * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination .= $this->generateCurrentStringForPage($counter);
					else
						$pagination .= $this->generateLinkStringForPage($counter);
				}
			}
		}
		//$pagination .=	'</ul>';
		//next button
		if ($page < $counter - 1)
		{
			$pagination .= '<a href="'.$this->PagiLinkUrl.'&p='.$next.'" text="下一頁 &gt;" style="padding: 0 3px;">下一頁</a>';
		}
		else
		{
			$pagination .= '<span class="disabled" style="padding: 0 3px;">下一頁</span>';
		}

		$pagination .= "</div>";


		return $pagination;
	}

	function generateCurrentStringForPage($page)
	{
		//return str_replace('%PAGE%', $page, $this->PagiCurrentHtml);
		return str_replace(array('%URL%', '%PAGE%'), array($this->PagiLinkUrl, $page), $this->PagiCurrentHtml);
	}
	function generateLinkStringForPage($page)
	{
		return str_replace(array('%URL%', '%PAGE%'), array($this->PagiLinkUrl, $page), $this->PagiLinkHtml);
	}
}
?>